import { View, Image, Text, Button } from "@tarojs/components";
import Taro from "@tarojs/taro";
import { useState } from "react";
import "./index.less";
import login_bg from "@/resource/login_bg.png";
import login_logo from "@/resource/login_logo.png";
import login_checked from "@/resource/login_checked.png";
import { fetchWxLogin, fetchLogin, fetchGetPhone, flushStorageUserInfo } from "@/services/login";
import { setToken } from "@/utils/auth";
export default function Login() {

  const [checked, setChecked] = useState(false)
  const getphonenumber = (e) => {
    const { code } = e.detail
    // Taro.login({
    //   success: async ({ code: js_code }) => {
    //     const { data: { data: { openId: openid } } } = await fetchWxLogin({ code: js_code })
    //     const { data: { msg: phone } } = await fetchGetPhone({ code })
    //     const { data } = await fetchLogin({ openid, phone })
    //     if (data.code == 200) {
    //       const { data: { token } } = data
    //       setToken(token)
    //       const res = await flushStorageUserInfo()
    //       Taro.showToast({
    //         title: '登录成功',
    //         icon: 'success',
    //         duration: 1000,
    //         success: async () => {
    //           setTimeout(() => {
    //             if (res === true) {
    //               Taro.switchTab({
    //                 url: '../index/index'
    //               })
    //             }
    //             else {
    //               Taro.navigateTo({
    //                 url: `../improveinfo/index`,
    //               });
    //             }
    //           }, 1000);
    //         }
    //       })
    //     }
    //   }
    // })

  };
  const uncheckedCallback = () => {
    if (!checked) {
      Taro.showToast({
        title: '请先阅读并勾选本页协议',
        icon: 'none'
      })
    }
  }
  return (
    <View className="loginpage">
      <Image className="bg" src={login_bg}></Image>
      <Image className="logo" src={login_logo}></Image>
      <Button className="btn"
        openType="getPhoneNumber"
        onGetPhoneNumber={getphonenumber}
        type="pramary"
        size="small"
      >立即登录
      </Button>
      {!checked ? <View className="btn_overlay" onClick={uncheckedCallback}></View> : ''}
      <View className="text">
        {checked ? (
          <Image className="checked" src={login_checked} onClick={() => setChecked(!checked)}></Image>
        ) : (
          <View className="check" onClick={() => setChecked(!checked)}></View>
        )}
        我已阅读并同意<Text>《平台用户协议》</Text>和<Text>《隐私政策》</Text>
      </View>
    </View>
  );
}
